<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="站点" prop="site">
        <el-input
          v-model="queryParams.site"
          placeholder="请输入站点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="村屯" prop="village">
        <el-input
          v-model="queryParams.village"
          placeholder="请输入村屯"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="农户" prop="farmer">
        <el-input
          v-model="queryParams.farmer"
          placeholder="请输入农户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="肥料名称" prop="fertilizerName">
        <el-input
          v-model="queryParams.fertilizerName"
          placeholder="请输入肥料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:growth:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:growth:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:growth:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:growth:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="growthList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="站点" align="center" prop="site" />
      <el-table-column label="村屯" align="center" prop="village" />
      <el-table-column label="农户" align="center" prop="farmer" />
      <el-table-column label="肥料名称" align="center" prop="fertilizerName" />
      <el-table-column label="亩用量" align="center" prop="fertilizerAmount" />
      <el-table-column label="揭膜培土" align="center" prop="filmRemoval" />
      <el-table-column label="株高" align="center" prop="plantHeight" />
      <el-table-column label="叶片数" align="center" prop="leafNumber" />
      <el-table-column label="最大叶长" align="center" prop="maxLeafLength" />
      <el-table-column label="最大叶宽" align="center" prop="maxLeafWidth" />
      <el-table-column label="排灌" align="center" prop="irrigationDrainage" />
      <el-table-column label="长势" align="center" prop="growthCondition" />
      <el-table-column label="整齐度" align="center" prop="uniformity" />
      <el-table-column label="病虫害" align="center" prop="diseasesPests" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:growth:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:growth:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改烟田旺长期对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="站点" prop="site">
          <el-input v-model="form.site" placeholder="请输入站点" />
        </el-form-item>
        <el-form-item label="村屯" prop="village">
          <el-input v-model="form.village" placeholder="请输入村屯" />
        </el-form-item>
        <el-form-item label="农户" prop="farmer">
          <el-input v-model="form.farmer" placeholder="请输入农户" />
        </el-form-item>
        <el-form-item label="肥料名称" prop="fertilizerName">
          <el-input v-model="form.fertilizerName" placeholder="请输入肥料名称" />
        </el-form-item>
        <el-form-item label="亩用量" prop="fertilizerAmount">
          <el-input v-model="form.fertilizerAmount" placeholder="请输入亩用量" />
        </el-form-item>
        <el-form-item label="揭膜培土" prop="filmRemoval">
          <el-input v-model="form.filmRemoval" placeholder="请输入揭膜培土" />
        </el-form-item>
        <el-form-item label="株高" prop="plantHeight">
          <el-input v-model="form.plantHeight" placeholder="请输入株高" />
        </el-form-item>
        <el-form-item label="叶片数" prop="leafNumber">
          <el-input v-model="form.leafNumber" placeholder="请输入叶片数" />
        </el-form-item>
        <el-form-item label="最大叶长" prop="maxLeafLength">
          <el-input v-model="form.maxLeafLength" placeholder="请输入最大叶长" />
        </el-form-item>
        <el-form-item label="最大叶宽" prop="maxLeafWidth">
          <el-input v-model="form.maxLeafWidth" placeholder="请输入最大叶宽" />
        </el-form-item>
        <el-form-item label="排灌" prop="irrigationDrainage">
          <el-input v-model="form.irrigationDrainage" placeholder="请输入排灌" />
        </el-form-item>
        <el-form-item label="长势" prop="growthCondition">
          <el-input v-model="form.growthCondition" placeholder="请输入长势" />
        </el-form-item>
        <el-form-item label="整齐度" prop="uniformity">
          <el-input v-model="form.uniformity" placeholder="请输入整齐度" />
        </el-form-item>
        <el-form-item label="病虫害" prop="diseasesPests">
          <el-input v-model="form.diseasesPests" placeholder="请输入病虫害" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGrowth, getGrowth, delGrowth, addGrowth, updateGrowth } from "@/api/system/growth";

export default {
  name: "Growth",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 烟田旺长期表格数据
      growthList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        site: null,
        village: null,
        farmer: null,
        fertilizerName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        site: [
          { required: true, message: "站点不能为空", trigger: "blur" }
        ],
        village: [
          { required: true, message: "村屯不能为空", trigger: "blur" }
        ],
        farmer: [
          { required: true, message: "农户不能为空", trigger: "blur" }
        ],
        fertilizerName: [
          { required: true, message: "肥料名称不能为空", trigger: "blur" }
        ],
        fertilizerAmount: [
          { required: true, message: "亩用量不能为空", trigger: "blur" }
        ],
        filmRemoval: [
          { required: true, message: "揭膜培土不能为空", trigger: "blur" }
        ],
        plantHeight: [
          { required: true, message: "株高不能为空", trigger: "blur" }
        ],
        leafNumber: [
          { required: true, message: "叶片数不能为空", trigger: "blur" }
        ],
        maxLeafLength: [
          { required: true, message: "最大叶长不能为空", trigger: "blur" }
        ],
        maxLeafWidth: [
          { required: true, message: "最大叶宽不能为空", trigger: "blur" }
        ],
        irrigationDrainage: [
          { required: true, message: "排灌不能为空", trigger: "blur" }
        ],
        growthCondition: [
          { required: true, message: "长势不能为空", trigger: "blur" }
        ],
        uniformity: [
          { required: true, message: "整齐度不能为空", trigger: "blur" }
        ],
        diseasesPests: [
          { required: true, message: "病虫害不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询烟田旺长期列表 */
    getList() {
      this.loading = true;
      listGrowth(this.queryParams).then(response => {
        this.growthList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        site: null,
        village: null,
        farmer: null,
        fertilizerName: null,
        fertilizerAmount: null,
        filmRemoval: null,
        plantHeight: null,
        leafNumber: null,
        maxLeafLength: null,
        maxLeafWidth: null,
        irrigationDrainage: null,
        growthCondition: null,
        uniformity: null,
        diseasesPests: null,
        remarks: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加烟田旺长期";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGrowth(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改烟田旺长期";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGrowth(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGrowth(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除烟田旺长期编号为"' + ids + '"的数据项？').then(function() {
        return delGrowth(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/growth/export', {
        ...this.queryParams
      }, `growth_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
